"""响应提取"""
import allure
import jsonpath

from utils.exception_utils import ExpException
from utils.jsonpath_utils import jsonpath_exp
from utils.logger_utils import *
from utils.request_utils import send_sql

class ExportUtils:
    def export_to_dict(self,*args,**kwargs):
        pass


class JsonExp(ExportUtils):

    def export_to_dict(self,param,response_data,r):
        try:
            if param["jsonExData"]:
                with allure.step("json提取"):
                    for k, v in eval(param["jsonExData"]).items():
                        response_data[k] = jsonpath_exp(r.json(),v)
        except Exception as e:
            info(f"提取失败！{e}")
            raise ExpException

    # def json_exp(param,response_data,r):
    #     try:
    #         if param["jsonExData"]:
    #             with allure.step("json提取"):
    #                 for k, v in eval(param["jsonExData"]).items():
    #                     response_data[k] = jsonpath_exp(r.json(),v)
    #     except Exception as e:
    #         info(f"提取失败！{e}")
    #         raise ExpException

def sql_exp(param,response_data):
    try:
        if param["sqlExData"]:
            with allure.step("sql提取"):
                for k, v in eval(param["sqlExData"]).items():
                    response_data[k] = send_sql(v)[k]
    except Exception as e:
        info(f"提取失败！{e}")
        raise ExpException
